"""
-*- coding: utf-8 -*-
@Author  : Link
@Time    : 2021/12/13 20:20
@Software: PyCharm
@File    : semi_stdf.py
@Remark  : 
"""
import os
from typing import Union

from Semi_ATE import STDF

from uitls.func import timestamp_to_str
from workspace_core.core_to_analysis_stdf.stdf_variable import GlobalVariable


class SemiStdfUtils:
    @staticmethod
    def is_std(file_name):
        suffix = os.path.splitext(file_name)[-1]
        if suffix not in GlobalVariable.STD_SUFFIXES:
            return False
        return True

    @staticmethod
    def get_mir(stdf) -> Union[dict, None]:
        mir_data = None
        try:
            for REC in STDF.records_from_file(stdf):
                if REC is None:
                    continue
                if REC.id == "MIR":
                    mir_data = REC.to_dict()
                    del mir_data['REC_LEN']
                    del mir_data['REC_TYP']
                    del mir_data['REC_SUB']
                    del mir_data['rec_id']
                    break
        except IndexError:
            pass
        return mir_data

    @staticmethod
    def get_cp_wafer_id(stdf) -> Union[str, None]:
        wafer_id = None
        i = 0
        try:
            for REC in STDF.records_from_file(stdf):
                i += 1
                if i > 1E3:
                    return None
                if REC is None:
                    continue
                if REC.id == "WIR":
                    return REC.to_dict().get("WAFER_ID", None)
        except IndexError:
            pass
        return wafer_id

    @staticmethod
    def get_mir_by_by_semi_ate(filepath: str) -> dict:
        if "DEMO" in filepath:
            return {
                "READ_FAIL": False,
                "PART_FLAG": 0,
                "LOT_ID": "DEMO",
                "SB_LOT_ID": "DEMO",
                "WAFER_ID": "DEMO",
                "SETUP_TIME": "2022-01-01 12:00:00",
                "START_TIME": "2022-01-01 14:00:00",
                "FLOW_ID": "CP1",
                "TEST_CODE": "R0",
                "PART_TYPE": "DEMOS",
                "JOB_NAME": "DEMOS",
                "EQUIPMENT": "S2",
                "TEMPERATURE": 85,
                "FILEPATH": filepath,
            }
        mir_data = None
        wafer_id = None
        i = 0
        for REC in STDF.records_from_file(filepath):
            i += 1
            if i > 3E2:
                break
            if REC is None:
                continue
            if REC.id == "MIR":
                mir_data = REC.to_dict()
            if REC.id == "WIR":
                wafer_id = REC.to_dict().get("WAFER_ID", None)
        return {
            "READ_FAIL": False,
            "PART_FLAG": 0,
            "LOT_ID": mir_data["LOT_ID"],
            "SB_LOT_ID": mir_data["SBLOT_ID"],
            "WAFER_ID": wafer_id,
            "SETUP_TIME": timestamp_to_str(mir_data["SETUP_T"]),
            "START_TIME": timestamp_to_str(mir_data["START_T"]),
            "FLOW_ID": mir_data["FLOW_ID"],
            "TEST_CODE": mir_data["TEST_COD"],
            "PART_TYPE": mir_data["PART_TYP"],
            "JOB_NAME": mir_data["JOB_NAM"],
            "EQUIPMENT": mir_data["NODE_NAM"],
            "TEMPERATURE": mir_data["TST_TEMP"],
            "FILEPATH": filepath,
        }
